package com.fastexport.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

public class ValidateUtils {

    private static final Logger logger = LoggerFactory.getLogger(FastExportUtils.class);

    public static void validateInputs(HttpServletResponse response, List<?> list) {
        if (list == null || list.isEmpty()) {
            logger.error("List is empty, unable to proceed with export.");
            throw new IllegalArgumentException("List must not be empty.");
        }
        if (response == null) {
            logger.error("HttpServletResponse is null, unable to proceed with export.");
            throw new IllegalStateException("HttpServletResponse must not be null.");
        }
    }

    public static void validateInputs(HttpServletResponse response, Map<String, List<?>> map) {
        if (map == null || map.isEmpty()) {
            logger.error("List is empty, unable to proceed with export.");
            throw new IllegalArgumentException("List must not be empty.");
        }
        if (response == null) {
            logger.error("HttpServletResponse is null, unable to proceed with export.");
            throw new IllegalStateException("HttpServletResponse must not be null.");
        }
    }

    public static String fileNameHandle(String fileName) {
        try {
            fileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20");
        } catch (UnsupportedEncodingException e) {
            logger.error("Failed to encode file name.", e);
            throw new RuntimeException("Failed to encode file name.", e);
        }
        return fileName;
    }

}
